﻿using KZ.Express.ENT;
using KZ.Express.Presenter;
using KZ.Express.Resource;
using KZ.Utilities;
using System;
using System.Data;
using System.Windows.Forms;

namespace KZ.Express.Setup.KZSystem
{
    public partial class RoleDetailView : FormBase, IRoleDetailView
    {
        #region Private Variables

        new RoleDetailPresenter _presenter;

        #endregion Private Variables

        #region Private Methods

        private void RoleDetailView_Load(object sender, EventArgs e)
        {
            LoadObject();
            LoadData();
        }

        private void dtgPermission_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == colPermission.Index)
            {
                PermissionView view = new PermissionView();
                view.ObjectType = Utility.StringParse(dtgPermission[colObjectType.Index, e.RowIndex].Value);
                view.PermissionID = Utility.StringParse(dtgPermission[colPermissionID.Index, e.RowIndex].Value);
                if (view.ShowDialog() == DialogResult.Yes)
                {
                    dtgPermission[colPermission.Index, e.RowIndex].Value = view.Permission;
                    dtgPermission[colPermissionID.Index, e.RowIndex].Value = view.PermissionID;
                }
            }
        }

        protected override void LoadData()
        {
            try
            {
                switch (base.Action)
                {
                    case ActionName.Update:
                        _presenter.LoadEntity();
                        break;
                }
            }
            catch (BusinessLogicException ex)
            {
                SetMessage(ex.Message, false);
            }
        }

        public void LoadObject()
        {
            try
            {
                SearchCriteria search = new SearchCriteria();
                search.Add("RoleID", this.EntityID);
                this.Objects = _presenter.LoadDataTable<ObjectENT>(search);
            }
            catch (BusinessLogicException ex)
            {
                SetMessage(ex.Message, false);
            }
        }

        #endregion Private Methods

        #region Constructors

        public RoleDetailView()
        {
            InitializeComponent();
            base.ObjectID = (int)KZ.Express.ENT.Objects.Role;
            base._presenter = _presenter = new RoleDetailPresenter(this);
        }

        #endregion Constructors

        #region Public Properties

        public override EntityBase Entity
        {
            get
            {
                RoleENT role = new RoleENT();
                role.RoleID = base.EntityID;
                role.RoleName = txtRoleName.Text;
                role.Description = txtDescription.Text;
                role.CreatedBy = UserENT.USERID;
                role.UpdatedBy = UserENT.USERID;

                return role;
            }
            set
            {
                if (value == null)
                {
                    ClearControl();
                    LoadData();
                }
                else
                {
                    RoleENT role = value as RoleENT;

                    txtRoleName.Text = role.RoleName;
                    txtDescription.Text = role.Description;
                }
            }
        }

        public DataTable Objects
        {
            get { return base._bds.DataSource as DataTable; }
            set
            {
                if (!value.Columns.Contains("Number"))
                    value.Columns.Add("Number", typeof(int));
                if (!value.Columns.Contains("Permission"))
                    value.Columns.Add("Permission", typeof(string));
                if (!value.Columns.Contains("PermissionID"))
                    value.Columns.Add("PermissionID", typeof(string));

                Global.GenerateNumber(ref value);
                base._bds.DataSource = value;
            }
        }

        #endregion Public Properties
    }
}